package com.bdzl.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bdzl.common.bean.BaseResponse;
import com.bdzl.daoRepository.AccidentDao;
import com.bdzl.daoRepository.ClauseDao;
import com.bdzl.daoRepository.CompanyDao;
import com.bdzl.exception.ParameterException;
import com.bdzl.pojo.Accident;
import com.bdzl.pojo.Clause;
import com.bdzl.pojo.Company;
import com.bdzl.request.AddAccident;
import com.bdzl.request.AddYiwai;
import com.bdzl.request.BdTj;
import com.bdzl.response.AllBd;
import com.bdzl.response.InfoAccident;
import com.bdzl.response.TkAcc;

@Service
public class DataService {
	@Autowired
	ClauseDao clauseDao;

	@Autowired
	CompanyDao companyDao;

	@Autowired
	AccidentDao accidentDao;

	private Integer num = 0;

	/**
	 * 查看数据列表
	 * 
	 * @param bdTj
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 * @throws Exception
	 */
	@Transactional(readOnly = true)
	public BaseResponse allcla(BdTj bdTj, Integer pageIndex, Integer pageSize) throws Exception {
		List<AllBd> data = new ArrayList<AllBd>();
		List<Clause> list;

		Company com = companyDao.findById(bdTj.getCompanyId());
		if (com == null) {
			if (bdTj.getType() > 0) {
				list = clauseDao.findbdTj(bdTj.getType(), bdTj.getSearch(), new PageRequest(pageIndex, pageSize));
			} else {
				list = clauseDao.findbdTj3(bdTj.getSearch(), new PageRequest(pageIndex, pageSize));
			}
		} else {
			if (bdTj.getType() > 0) {
				list = clauseDao.findbdTj2(com, bdTj.getType(), bdTj.getSearch(), new PageRequest(pageIndex, pageSize));
			} else {
				list = clauseDao.findbdtj4(com, bdTj.getSearch(), new PageRequest(pageIndex, pageSize));
			}
		}

		for (Clause clause : list) {
			AllBd a = new AllBd();

			a.setInsuranceId(clause.getId());
			a.setCompanyName(clause.getCompany().getName());
			a.setCode(clause.getNumber());
			a.setSearch(clause.getSearchName());
			a.setPolicy(clause.getName());
			a.setType(clause.getType());
			data.add(a);
		}

		BaseResponse br = new BaseResponse();
		br.setData(data);
		br.setStateCode(101);
		br.setStateMessage("获取信息成功");

		return br;
	}

	/**
	 * 新建意外保障
	 * 
	 * @param addYiwai
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse newyiwai(Integer adminId, AddYiwai addYiwai) throws Exception {
		// 先添加保单 信息，
		Company com = companyDao.findById(addYiwai.getCompanyId());
		if (com == null) {
			throw new ParameterException(301, "你选择的公司不存在");
		}
		Clause cla = new Clause();
		cla.setCompany(com);
		cla.setType(addYiwai.getType());
		cla.setName(addYiwai.getInsuranceName());
		cla.setSearchName(addYiwai.getSearchName());
		cla.setAddress(addYiwai.getDownloadAddress());
		cla.setAdditionals(addYiwai.getAdditionalNumber());
		cla.setState(1);
		cla.setMain(1);
		cla.setTime(System.currentTimeMillis());
		cla.setUserId(adminId);
		cla.setClickNum(0);
		// 设置保单编号 公司简称加 0000；
		Integer numsize = num.toString().length();
		String number = "";
		for (int i = 0; i < 4 - numsize; i++) {
			number = "0" + number;
		}
		number = number + num;
		cla.setNumber(com.getIntroduction() + number);
		clauseDao.save(cla);
		num++;

		// 添加意外保障数据
		List<AddAccident> insuranceList = addYiwai.getInsuranceList();
		for (AddAccident addAccident : insuranceList) {
			Accident a = new Accident();
			a.setClause(cla);
			a.setLabel(addAccident.getInsuranceTitle());
			a.setContent(addAccident.getLiabilityAbstract());
			a.setInformation(addAccident.getAbstractDetail());
			a.setProportion(addAccident.getCoefficient());
			a.setType(addAccident.getClassification());

			accidentDao.save(a);
		}

		BaseResponse br = new BaseResponse();
		br.setStateCode(100);
		br.setStateMessage("添加数据成功");

		return br;

	}

	/**
	 * 删除保单
	 * 
	 * @param insuranceId
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse delcla(Integer insuranceId, Integer adminId) throws Exception {
		// 修改保单是否显示
		Clause cla = clauseDao.findById(insuranceId);
		if (cla == null) {
			throw new ParameterException(301, "保单不存在");
		}
		Long now = System.currentTimeMillis();

		clauseDao.update(insuranceId, now, adminId);

		BaseResponse br = new BaseResponse();
		br.setStateCode(103);
		br.setStateMessage("删除成功");

		return br;

	}

	/**
	 * 查看意外保障详情
	 * 
	 * @param insuranceId
	 * @return
	 * @throws Exception
	 */
	@Transactional(readOnly = true)
	public BaseResponse infoaccident(Integer insuranceId) throws Exception {
		TkAcc data = new TkAcc();

		Clause cla = clauseDao.findById(insuranceId);
		if (cla == null) {
			throw new ParameterException(301, "保单不存在");
		}
		if (cla.getType() != 1) {
			throw new ParameterException(301, "请求类型错误");
		}
		data.setInsuranceCode(cla.getNumber());
		data.setCompanyName(cla.getCompany().getName());
		data.setType(cla.getType());
		data.setInsuranceName(cla.getName());
		data.setSearchName(cla.getSearchName());
		data.setDownloadAddress(cla.getAddress());
		data.setAdditionalNumber(cla.getAdditionals());

		// 意外保障摘要信息集合
		List<InfoAccident> insuranceList = new ArrayList<InfoAccident>();

		// 获取信息
		List<Accident> list = accidentDao.findByClause(cla);

		for (Accident accident : list) {
			InfoAccident a = new InfoAccident();
			a.setAccidentId(accident.getId());
			a.setInsuaranceTitle(accident.getLabel());
			a.setLiabilityAbstract(accident.getContent());
			a.setAbstractDetail(accident.getInformation());
			a.setCoefficient(accident.getProportion());
			a.setClassification(accident.getType());

			insuranceList.add(a);
		}

		data.setInsuranceList(insuranceList);

		BaseResponse br = new BaseResponse();
		br.setData(data);
		br.setStateCode(101);
		br.setStateMessage("获取信息成功");

		return br;
	}

	
	
	
	
	
	
	/**
	 * 
	 * @param insuranceId
	 * @param adminId
	 * @return
	 * @throws Exception
	 */
	@Transactional(rollbackFor = Exception.class)
	public BaseResponse updaccident(Integer insuranceId, Integer adminId) throws Exception {
		
		
		
		
	
		BaseResponse br = new BaseResponse();
		br.setStateCode(102);
		br.setStateMessage("修改成功");

		return br;

	}
	
	
}
